<?php

namespace App\Http\Controllers\Admin\Ocean;

use App\Common\Controllers\Admin\AdminController;
use App\Common\Helpers\Functions;
use App\Common\Tools\CustomException;
use App\Models\Ocean\OceanAccountModel;
use Illuminate\Http\Request;

class OceanController extends AdminController
{
    /**
     * constructor.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 列表预处理
     */
    public function selectPrepare(){
        $this->curdService->selectQueryBefore(function(){
            $this->filter();
        });

        $this->curdService->selectQueryBefore(function(){
            $this->curdService->customBuilder(function($builder){
                $builder->withPermission();
            });
        });
    }

    /**
     * 查询（无分页）预处理
     */
    public function getPrepare(){
        $this->curdService->getQueryBefore(function (){
            $this->filter();
        });

        $this->curdService->getQueryBefore(function(){
            $this->curdService->customBuilder(function($builder){
                $builder->withPermission();
            });
        });
    }

    /**
     * 树预处理
     */
    public function treePrepare(){
        $this->curdService->treeQueryBefore(function(){
            $this->curdService->customBuilder(function($builder){
                $builder->withPermission();
            });
        });
    }



    /**
     * @return mixed
     * 获取当前管理员账户
     */
    public function getCurrentAdminUserAccount(){
        $adminUserInfo = Functions::getGlobalData('admin_user_info');
        return $this->getAdminUserAccount($adminUserInfo);
    }

    /**
     * @param $adminUserInfo
     * @return mixed
     * 获取管理员账户
     */
    public function getAdminUserAccount($adminUserInfo){
        $oceanAccountModel = new OceanAccountModel();
        //if(!$adminUserInfo['is_admin']){
            $oceanAccountModel = $oceanAccountModel->where('admin_id', $adminUserInfo['admin_user']['id']);
        //}
        $oceanAccounts = $oceanAccountModel->get();

        return $oceanAccounts;
    }

    /**
     * @param $accountId
     * @return mixed
     * @throws CustomException
     * 获取已授权账户
     */
    public function getAccessAccount($accountId){
        // 获取当前管理员账户
        $oceanAccounts = $this->getCurrentAdminUserAccount();

        // 查找
        $oceanAccount = $oceanAccounts->where('account_id', $accountId)->first();
        if(empty($oceanAccount)){
            throw new CustomException([
                'code' => 'NOT_ACCESS_ACCOUNT',
                'message' => '你不能操作该账户',
            ]);
        }

        return $oceanAccount;
    }


    /**
     * 过滤
     */
    protected function filter(){
        $this->curdService->customBuilder(function($builder){
            // 关键词
            $keyword = $this->curdService->requestData['keyword'] ?? '';
            if(!empty($keyword)){
                $primaryKey = $this->model->getKeyName();
                $builder->whereRaw("({$primaryKey} LIKE '%{$keyword}%' OR name LIKE '%{$keyword}%')");
            }
        });
    }
}
